package hms.database;

import hms.employees.TerminationReason;
import hms.employees.TerminationReasonList;

import java.util.Iterator;

public class DBRowListObjecttest {
	public static void main(String args[]) {

		// Preliminary: Clean the DB table
		{
			TerminationReasonList trList = new TerminationReasonList();
			trList.fillFromDatabase();

			System.out.println("Cleaning table...");
			for(TerminationReason tr : trList) {
				System.out.println("Deleting: ["+tr.getUniqueIdentifier()+": "+tr+"]");
				tr.markToBeDeleted();
			}
			trList.commitToDatabase();
			System.out.println();
		}

		// Test 1: Add new strings to database
		{
			TerminationReasonList trList = new TerminationReasonList();

			trList.add(new TerminationReason("Laid off"));
			trList.add(new TerminationReason("Arrested for drug use"));
			trList.add(new TerminationReason("Relocated"));

			trList.commitToDatabase();
			System.out.printf("Added three strings to database\n\n");
		}

		// Test 2: Read and change database
		{
			TerminationReasonList trList = new TerminationReasonList();

			trList.fillFromDatabase();

			System.out.println("Printing current contents of DB as read.");
			Iterator<TerminationReason> i = trList.iterator();
			TerminationReason tr;
			while(i.hasNext()) {
				tr = i.next();
				System.out.println(tr.getUniqueIdentifier()+": "+tr);
			}

			TerminationReason reason = trList.get(0);
			reason.setTerminationReason("Downsizing casualty");

			System.out.println("\nChanged \"Laid off\" to \"Downsizing casualty\". Committing changes...\n");
			trList.commitToDatabase();

			System.out.println("Status of trList:");
			i = trList.iterator();
			while(i.hasNext()) {
				tr = i.next();
				System.out.println(tr.getUniqueIdentifier()+": "+tr);
			}
			System.out.println();
		}

		// Test 3: Re-print database, pull individual item
		{
			TerminationReasonList trList = new TerminationReasonList();

			trList.fillFromDatabase();

			System.out.println("Reprinting database (should match above)...");
			Iterator<TerminationReason> i = trList.iterator();
			TerminationReason tr;
			while(i.hasNext()) {
				tr = i.next();
				System.out.println(tr.getUniqueIdentifier()+": "+tr);
			}

			System.out.println("\nPulling Unique ID 1 from DB...");
			TerminationReason reason2 = new TerminationReason(1);
			System.out.println("Unique ID #1: " + reason2 );
		}

	// Test 4: Add duplicates
		{
			TerminationReasonList trList = new TerminationReasonList();

			System.out.println("Attempting to create new and duplicate entries...\n");
			trList.add(new TerminationReason("Laid off"));
			trList.add(new TerminationReason("Arrested for drug use"));
			trList.add(new TerminationReason("Relocated"));
			trList.add(new TerminationReason("Abducted by aliens"));

			trList.commitToDatabase();
			System.out.println("Committed new/duplicates to database.\n");
			
			System.out.println("Status of trList:");
			Iterator<TerminationReason> i = trList.iterator();
			TerminationReason tr;
			while(i.hasNext()) {
				tr = i.next();
				System.out.println(tr.getUniqueIdentifier()+": "+tr);
			}
			
			System.out.println("\nStatus of DB:");
			trList = new TerminationReasonList();
			trList.fillFromDatabase();
			i = trList.iterator();
			while(i.hasNext()) {
				tr = i.next();
				System.out.println(tr.getUniqueIdentifier()+": "+tr);
			}
		}

	}
}
